package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {

    /**
     * 查询总数
     *
     * @return
     */
    @Select("select count(*) from emp")
    Long count();

    /**
     * 分页查询
     *
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select e.*, d.name deptName from emp e left join dept d on e.dept_id=d.id limit #{start}, #{pageSize}")
    List<Emp> selectPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize);

    /**
     *  查询全部
     * @return
     */
    @Select("select e.*, d.name deptName from emp e left join dept d on e.dept_id=d.id")
    Page<Emp> list();

    /**
     * 分页条件查询
     *
     * @param empQueryParam
     * @return
     */
    Page<Emp> pageQuery(EmpQueryParam empQueryParam);

    /**
     * 新增员工数据
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into emp(username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
            "values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    void insert(Emp emp);

    /**
     * 编辑员工
     */
    @Update("update emp set username=#{username}, name=#{name}, gender=#{gender}, phone=#{phone}, job=#{job}, salary=#{salary}, image=#{image}, entry_date=#{entryDate}, dept_id=#{deptId}, update_time=#{updateTime} where id=#{id}")
    void update(Emp emp);

    /**
     * 根据id删除员工
     */
    @Delete("delete from emp where id=#{id}")
    void deleteById(Integer id);

    /**
     * 查询全部员工
     */
    @Select("select * from emp")
    List<Emp> findAll();

    /**
     * 批量删除员工信息
     */
    void deleteByIds(List<Integer> ids);

    /**
     *  根据id查询员工信息
     * @param id
     * @return
     */
    Emp getById(Integer id);

    /**
     *  根据id更新员工信息
     * @param emp
     */
    void updateById(Emp emp);

    /**
     * 统计各个职位的员工人数
     */
    @MapKey("pos")
    List<Map<String, Object>> countEmpJobData();

    /**
     * 统计员工性别信息
     */
    @MapKey("name")
    List<Map> countEmpGenderData();

    /**
     *  登录
     * @param emp
     * @return
     */
    @Select("select*from emp where username=#{username} and password=#{password}")
    Emp getUsernameAndPassword(Emp emp);

    @Select("select count(*) from emp where dept_id=#{deptId} limit 1")
    Integer existsWithClazzId(Integer id);
}